<?php

class ModuleResultOfDailySummary extends CmsCustomModule {
    
    //set the public head
    private function setHeadString($condition=null,$sort_by=null,$rand_code) {
        
        $date_list = $condition['date'];
        $current_time = date('Y-m-d H:i:s');

        $condition_list = '<br/>'.
                          '<div><strong>Date: </strong>'.$date_list.'</div>';

       //control tag.                         
        $export_tag =  'Export to '.AC::exportHtmlControl($rand_code,AC::EXPORT_FILE_PREFIX_NAME_DAILY_SUMMARY)
                                   .AC::exportPdfControl($rand_code,AC::EXPORT_FILE_PREFIX_NAME_DAILY_SUMMARY)
                                   .AC::exportCvsControl($rand_code,AC::EXPORT_FILE_PREFIX_NAME_DAILY_SUMMARY);

        $head_string = '<div class="hd">'.
                       '    <div class="utility">'.
                       '        <a href="#" class="print">Print</a> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'.$export_tag.
                       '    </div>'.
                       '    <hr>'.
                       '    <h1>'.$this->title.'</h1>'.
                       '    <div class="ui-helper-clearfix">'.
                       '        <div class="time">'.$current_time.'</div>'.
                       '        <ul>'.$condition_list.'</ul>'.
                       '    </div>'.
                       '</div>';

        $head_string_exprot = '<div class="hd">'.
                                '    <div class="utility">'.
                                '        <a href="#" class="print">Print</a> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'.
                                '    </div>'.
                                '    <hr>'.
                                '    <h1>'.$this->title.'</h1>'.
                                '    <div class="ui-helper-clearfix">'.
                                '        <div class="time">'.$current_time.'</div>'.
                                '        <ul>'.$condition_list.'</ul>'.
                                '    </div>'.
                                '</div>';

        return array('view'=>$head_string,'export'=>$head_string_exprot);
    }

    //out put the body string.
    private function getOutPutBody($head_string,$body_string) {
        $output ='<div id="result" class="os_browser_result" >'.$head_string.
                 '  <div class="bd">'.
                    $body_string.
                 '  </div>'.
                 '</div>';
        return $output;
    }

    //get the page search filter condition.
    private function getPageCondition($params) {
        $date = date('Y-m-d');
        if (!empty($params['date'])) {
            $date = $params['date'];
        }
        return array('condition'=>array('date'=>$date));
    }        
    
    //view viewing frequency
    private function viewFrequencyBodyString($date) {
        $date = (!empty($date) && strtotime($date)>0) ? $date : date('Y-m-d');
        $data = ModelDailySummary::getInstance()->getViewFrequencyData($date);
        $body_list ='<table border="0" cellpadding="0" cellspacing="0">'.
	                '<tbody>'.
                    '   <tr>'.
                    '       <td colspan="4" class="tit">'.
                    '           <div align="center" class="STYLE1">'.AC::DAILY_SUMMARY_VIEW_FREQUENCY.'</div>'.
                    '       </td>'.
                    '   </tr>'.
                    '   <tr class="weight">'.
                    '       <td bgcolor="#cccccc"><strong>Date</strong></td>'.
                    '       <td bgcolor="#cccccc" class="num"><strong>No. of Stream</strong></td>'.
                    '   </tr>'.
                    '   <tr><td>'.$date.'</td><td class="num">'.number_format($data).'</td></tr>'.
                    '</tbody>'.
                    '</table>';
        return $body_list;
    }

    //Total Streaming Duration with bitrate
    private function totalStreamingDurationWithBitrateBodyString($date) {
        $date = (!empty($date) && strtotime($date)>0) ? $date : date('Y-m-d');
        $data = ModelDailySummary::getInstance()->getTotalDurationWithBitrate($date);
        $body_list ='<table border="0" cellpadding="0" cellspacing="0">'.
                    '<tbody>'.
                    '   <tr>'.
                    '       <td colspan="4" class="tit"><div align="center" class="STYLE1">'.AC::DAILY_SUMMARY_TOTAL_BITRATE.'</div></td>'.
                    '   </tr>'.
                    '   <tr class="weight">'.
                    '       <td bgcolor="#cccccc"><strong>Bitrate</strong></td>'.
                    '       <td bgcolor="#cccccc" class="num"><strong>Duration</strong></td>'.
                    '   </tr>';
        foreach($data as $list) {
            $body_list .= '<tr><td>'.$list['bitrate'].'K</td><td class="num">'.number_format($list['Total']).'</td></tr>';
        }
        $body_list .='</tbody></table>';
        return $body_list;
    }

    //Number of unqiue user.
    private function numberOfUniqueUserBodyString($date) {
        $date = (!empty($date) && strtotime($date)>0) ? $date : date('Y-m-d');
        $data = ModelDailySummary::getInstance()->getUniqueUserData($date);
        $body_list ='<table border="0" cellpadding="0" cellspacing="0">'.
	                '<tbody>'.
                    '   <tr>'.
                    '       <td colspan="4" class="tit"><div align="center" class="STYLE1">'.AC::DAILY_SUMMARY_UNIQUE_USER.'</div></td>'.
                    '   </tr>'.
                    '   <tr class="weight">'.
                    '       <td bgcolor="#cccccc"><strong>Date</strong></td>'.
                    '       <td bgcolor="#cccccc" class="num"><strong>unique user</strong></td>'.
                    '   </tr>'.
                    '   <tr><td>'.$date.'</td><td class="num">'.number_format($data).'</td></tr>'.
                    '</tbody></table>';
        return $body_list;
    }

    //Subscriber with package
    private function subscriberWithPackageBodyString($date) {
        $date = (!empty($date) && strtotime($date)>0) ? $date : date('Y-m-d');
        $data = ModelDailySummary::getInstance()->getSubscriberPackageData($date);
        if (!empty($data)) {
            $package_ids_arr = AC::getInstance()->getConfigSetting(AC::$video_plan_package);
        }
        $body_list ='<table border="0" cellpadding="0" cellspacing="0">'.
	                '<tbody>'.
                    '   <tr>'.
                    '       <td colspan="5" class="tit"><div align="center" class="STYLE1">'.AC::DAILY_SUMMARY_SUBSCRIPTION.'</div></td>'.
                    '   </tr>'.
                    '   <tr class="weight">'.
                    '       <td bgcolor="#cccccc"><strong>Subscriber package</strong></td>'.
                    '       <td bgcolor="#cccccc" class="num"><strong>Total</strong></td>'.
                    '       <td bgcolor="#cccccc" class="num"><strong>New</strong></td>'.
                    '       <td bgcolor="#cccccc" class="num"><strong>Renew</strong></td>'.
                    '       <td bgcolor="#cccccc" class="num"><strong>Churn</strong></td>'.
                    '   </tr>';
        foreach($data as $key=>$list) {
            $body_list .='<tr>'.
                        '   <td>'.$package_ids_arr[$key].'</td>'.
                        '   <td class="num">'.number_format($list['total']).'</td>'.
                        '   <td class="num">'.number_format($list['new']).'</td>'.
                        '   <td class="num">'.number_format($list['renew']).'</td>'.
                        '   <td class="num">'.number_format($list['churn']).'</td>'.
                        '</tr>';
        }
        $body_list .='</tbody></table>';
        return $body_list;
    }

    //number of errors reported.
    private function numberOfErrorsReportedBodyString($date) {
        $date = (!empty($date) && strtotime($date)>0) ? $date : date('Y-m-d');
        //$data = ModelDailySummary::getInstance()->getSubscriberPackageData($date);
        $body_list ='<table border="0" cellpadding="0" cellspacing="0">'.
	                '<tbody>'.
                    '   <tr>'.
                    '       <td colspan="4" class="tit"><div align="center" class="STYLE1">Number of Errors reported</div></td>'.
                    '   </tr>'.
                    '   <tr class="weight">'.
                    '       <td bgcolor="#cccccc"><strong>Date</strong></td>'.
                    '       <td bgcolor="#cccccc" class="num"><strong>Error</strong></td>'.
                    '   </tr>'.
                    '   <tr><td>'.$date.'</td><td class="num">0</td></tr>'.
                    '</tbody>'.
                    '</table>';
        return $body_list;
    }

    private function setDailySummaryBodyString($condition) {
        $body_list = null;
        $body_list .= $this->viewFrequencyBodyString($condition['date']).'<br/>';
        $body_list .= $this->totalStreamingDurationWithBitrateBodyString($condition['date']).'<br/>';
        $body_list .= $this->numberOfUniqueUserBodyString($condition['date']).'<br/>';
        $body_list .= $this->subscriberWithPackageBodyString($condition['date']).'<br/>';
        $body_list .= $this->numberOfErrorsReportedBodyString($condition['date']).'<br/>';
        return $body_list;
    }

    protected function __renderHead() {
        $body_string = null;
        $rand_code = rand(10000,99999);

        //parameters re-assignment.
        $conditions = $_GET;
        $conditions = $this->getPageCondition($conditions);

        //get the header string.
        $head_string = $this->setHeadString($conditions['condition'],null,$rand_code);
        
        //body string.
        $body_string .= $this->setDailySummaryBodyString($conditions['condition']);

        //write a tmp file Export Tool.
        $export_html_string = $this->getOutPutBody($head_string['export'],$body_string);
        $write_result = AC::getInstance()->writeTempBodyToFile($rand_code,$export_html_string);
        
        //output.
        $this->contents =$this->getOutPutBody($head_string['view'],$body_string);
    }

}


?>